package ua.com.globallogic.basecamp.krasnyanskiy.anagrams;

import ua.com.globallogic.basecamp.krasnyanskiy.anagrams.ioutils.AnagramFileReader;
import ua.com.globallogic.basecamp.krasnyanskiy.anagrams.ioutils.AnagramFileWriter;
import ua.com.globallogic.basecamp.krasnyanskiy.anagrams.ioutils.FileValidationException;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {

        // input
        System.out.print("Enter file to read: ");
        Scanner scanner = new Scanner(System.in);
        String fileName = scanner.nextLine();

        // read file
        AnagramFileReader reader = new AnagramFileReader();
        String str;
        try {
            str = reader.read(new File(fileName));
        } catch (FileValidationException e) {
            System.out.println(e.getMessage());
            return;
        }

        // parse the input
        StringParser parser = new StringParser();
        List<String> words = parser.parse(str);

        // check an anagram
        AnagramFinder finder = new AnagramFinder();
        Set<Anagram> anagrams = finder.findAnagrams(words);

        // write to file
        AnagramFileWriter writer = new AnagramFileWriter();
        writer.write(anagrams);
    }
}